package com.bao.interkont.controller;

import com.bao.interkont.pojo.Result;
import com.bao.interkont.pojo.User;
import com.bao.interkont.server.LoginServer;
import com.bao.interkont.server.UserServer;
import com.bao.interkont.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bao*
 * @version 1.0
 */
@RestController
public class LoginController {
    @Autowired
    private LoginServer loginServer;
    @Autowired
    private UserServer userServer;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if(!loginServer.login(user)) {
            return Result.error("账号或密码错误");
        }
        User user1 = getUserByAccount(user.getAccount());
        Map<String,Object> map = new HashMap<>();
        map.put("userName",user1.getUserName());
        map.put("uid",user1.getUid());
        map.put("img",user1.getImg());
        String jwt = JwtUtils.generateJwt(map);
        return Result.success(jwt);
    }

    private User getUserByAccount(String account) {
        User user = userServer.isAccountSame(account);
        return user;
    }
}
